package controllers

import (
	"jiabo/appframework/libs/autoNumber"
	"jiabo/appframework/libs/taskmanage"
)

type CreateOrg struct {
	RootController
}

func (this *CreateOrg) Post() {
	username := this.CheckSession()
	orgname := this.GetString("orgname")
	orgdesc := this.GetString("orgdesc")

	org := taskmanage.Orgnization{}
	org.OrgName = orgname
	org.Desc = orgdesc
	org.Owner = username.(string)
	ifexist, _ := org.CheckIfOrgnizationExistbyName()
	if ifexist {
		this.Ctx.WriteString("err")
	} else {
		a, _ := autoNumber.GetFormattedNumber("orgnization")
		org.Oid = a
		err2 := org.Save()
		if err2 != nil {
			this.Ctx.WriteString("err")
		} else {
			userOrg := taskmanage.UserOrg{}
			userOrg.UserName = username.(string)
			userOrg.OrgOid = a
			err3 := userOrg.Save()
			if err3 != nil {
				this.Ctx.WriteString("err")
			} else {
				this.Ctx.WriteString("ok")
			}
		}

	}

}
